const app = getApp()
const constant = require("../../../../../../constant.js")
const storage = require("../../../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu
    })
    this.data.host = constant.api(app.globalData.pord).url;
    this.data.unionid = wx.getStorageSync(storage.keys.unionid);
    this.data.courseRoundId = options.courseRoundId;
    this.data.batchCoursewareId = options.batchCoursewareId;
    this.data.dailyCoursewareId = options.dailyCoursewareId;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

    wx.request({
      url: that.data.host + '/profile/courses/' + that.data.courseRoundId + '/weekly-plans/' + that.data.batchCoursewareId + '/daily-coursewares/' + that.data.dailyCoursewareId + '?unionid=' + that.data.unionid,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          wx.setNavigationBarTitle({
            title: res.data.data.title
          });

          that.setData({
            daily: [
              {
                type: '热身运动',
                coursewares: res.data.data.warm_up
              },
              {
                type: '正式运动',
                coursewares: res.data.data.formal
              },
              {
                type: '拉伸运动',
                coursewares: res.data.data.stretch
              }
            ]
          });
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onCoursewareClick: function (e) {
    var coursewareId = e.currentTarget.dataset.id;

    if (this.data.coursewareId == coursewareId) {
      this.setData({
        coursewareId: null
      })
    } else {
      this.setData({
        coursewareId: coursewareId
      })
    }
  }
})